package com.xuan.system.controller;


import com.xuan.common.core.controller.BaseController;
import com.xuan.common.core.domain.R;
import com.xuan.common.core.domain.TableDataInfo;
import com.xuan.common.core.exception.BusinessException;
import com.xuan.system.domain.contest.dto.*;
import com.xuan.system.domain.contest.vo.ContestDetailVo;
import com.xuan.system.service.contest.ContestService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/contest")
public class ContestController extends BaseController {


    // 竞赛管理
    // B端: 查看竞赛列表, 编辑, 删除, 发布, 新增竞赛
    // C端: 查看竞赛列表(未完赛, 历史竞赛), 报名竞赛, 参加竞赛(完成竞赛, 查看下一题), 我的比赛, 查看竞赛排名, 我的消息
    @Resource
    private ContestService contestService;


    @GetMapping("/list")
    public TableDataInfo listContest(ContestQueryDTO contestQueryDTO) {
        if (contestQueryDTO == null) {
            throw new BusinessException("参数为空");
        }
        return contestService.listContest(contestQueryDTO);
    }


    @PostMapping("/add")
    public R<String> addContest(@RequestBody ContestAddDTO contestAddDTO) {
        if (contestAddDTO == null) {
            throw new BusinessException("参数为空");
        }
        return R.ok(contestService.addContest(contestAddDTO));
    }


    @PostMapping("/question/add")
    public R<Void> addContestQuestion(@RequestBody ContestQuestionAddDTO contestQuestionDTO) {
        if (contestQuestionDTO == null) {
            throw new BusinessException("参数为空");
        }
        return toR(contestService.addContestQuestion(contestQuestionDTO));
    }


    @GetMapping("/detail")
    public R<ContestDetailVo> getContestDetail(Long contestId) {
        if (contestId == null || contestId < 1) {
            throw new BusinessException("参数为空");
        }
        return R.ok(contestService.getContestDetail(contestId));
    }

    @PutMapping("/edit")
    public R<Void> editContest(@RequestBody ContestEditDTO contestEditDTO) {
        if (contestEditDTO == null) {
            throw new BusinessException("参数为空");
        }
        return toR(contestService.editContest(contestEditDTO));
    }


    @DeleteMapping("/question/delete")
    public R<Void> deleteContestQuestion(Long contestId, Long questionId) {
        if (contestId == null || questionId == null || contestId < 1 || questionId < 1) {
            throw new BusinessException("参数为空");
        }
        return toR(contestService.deleteContestQuestion(contestId, questionId));
    }

    @DeleteMapping("/delete")
    public R<Void> deleteContest(Long contestId) {
        if (contestId == null || contestId < 1) {
            throw new BusinessException("参数为空");
        }
        return toR(contestService.deleteContest(contestId));
    }


    // 三种情况可以发布竞赛:新增竞赛页面,编辑竞赛页面,竞赛列表页面
    // 发布竞赛的前提: 竞赛存在 & 竞赛存在题目
    @PutMapping("/publish")
    public R<Void> publishContest(Long contestId) {
        return toR(contestService.publishContest(contestId));
    }


    @PutMapping("/cancelPublish")
    public R<Void> cancelPublish(Long contestId) {
        if (contestId == null || contestId < 1) {
            throw new BusinessException("参数为空");
        }
        return toR(contestService.cancelPublish(contestId));
    }


}
